/*
 * CCVisu is a tool for visual graph clustering
 * and general force-directed graph layout.
 * This file is part of CCVisu.
 *
 * Copyright (C) 2005-2011  Dirk Beyer
 *
 * CCVisu is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * CCVisu is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with CCVisu; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Please find the GNU Lesser General Public License in file
 * license_lgpl.txt or http://www.gnu.org/licenses/lgpl.txt
 *
 * Dirk Beyer    (firstname.lastname@uni-passau.de)
 * University of Passau, Bavaria, Germany
 */
package org.sosy_lab.ccvisu.readers.factextractor;

import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.EnumDeclaration;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;

public class EclipseFactVisitor extends ASTVisitor {

  private static final String RELATION_NAME_PACKAGE_REFERENCE = "refPackage";

  private static final String RELATION_NAME_CLASS_REFERENCE = "refClass";

  private static final String RELATION_NAME_FUNCTIONCALL = "REFfunction";

  private static final String RELATION_NAME_INTERFACE_IMPLEMENTATION = "implements";

  private static final String RELATION_NAME_INHERITANCE = "inheritClass";

  private static final String RELATION_NAME_PACKAGE_CONTAINS = "pckgContains";

  private ProjectStructureInfo projectStructureInfo;

  private ITypeBinding         currentTypeBinding;

  private MethodDeclaration    currentMethod;

  void reset() {
    currentTypeBinding = null;
    currentMethod = null;
  }

  EclipseFactVisitor(ProjectStructureInfo projectStructureInfo) {
    this.projectStructureInfo = projectStructureInfo;
  }

  @Override
  public boolean visit(EnumDeclaration pNode) {
    String pSourceName = EclipseFactUtils.getEnumFullName(pNode);

    //    projectStructureInfo.addNodeType(pSourceName, NodeType.ENUM);

    // Add package relation
    pSourceName = EclipseFactUtils.getPackageName(pNode);
    String pTargetName = EclipseFactUtils.getEnumFullName(pNode);
    projectStructureInfo.addRelation(RELATION_NAME_PACKAGE_CONTAINS,
        pSourceName, pTargetName, EclipseFactUtils.getPackageName(pNode));

    return true;
  }

  @Override
  public boolean visit(TypeDeclaration pNode) {
    assert pNode != null;

    currentTypeBinding = pNode.resolveBinding();

    // Add inheritance relation
    String pSourceName = EclipseFactUtils.getTypeFullName(pNode);
    String pTargetName = EclipseFactUtils.getTypeFullName(pNode.getSuperclassType());
    projectStructureInfo.addRelation(RELATION_NAME_INHERITANCE, pSourceName,
        pTargetName, EclipseFactUtils.getPackageName(pNode.getSuperclassType()));

    //    if (pNode.isInterface()) {
    //      projectStructureInfo.addNodeType(pSourceName, NodeType.INTERFACE);
    //    } else {
    //      projectStructureInfo.addNodeType(pSourceName, NodeType.CLASS);
    //    }
    //
    // It can happen that the type is situated in the (default) package, hence, we cannot
    // use the visit(PackageDeclaration pNode) method to identify it.
    //    projectStructureInfo.addNodeType(EclipseFactUtils.getPackageName(pNode), NodeType.PACKAGE);

    // Add super interfaces relations
    for (Object interfaceType : pNode.superInterfaceTypes()) {
      pTargetName = EclipseFactUtils.getTypeFullName((Type) interfaceType);
      projectStructureInfo.addRelation(RELATION_NAME_INTERFACE_IMPLEMENTATION,
          pSourceName, pTargetName,
          EclipseFactUtils.getPackageName((Type) interfaceType));
    }

    // Add 'pckgContains' relation
    pSourceName = EclipseFactUtils.getPackageName(pNode);
    pTargetName = EclipseFactUtils.getTypeFullName(pNode);
    projectStructureInfo.addRelation(RELATION_NAME_PACKAGE_CONTAINS,
        pSourceName, pTargetName, EclipseFactUtils.getPackageName(pNode));

    return true;
  }

  @Override
  public boolean visit(MethodDeclaration pNode) {
    if (currentMethod != null) { return false; }
    currentMethod = pNode;

    // EclipseFactUtils.getMethodFullName(currentMethod);
    // projectStructureInfo.addNodeType(pSourceName, NodeType.METHOD);

    return true;
  }

  @Override
  public void endVisit(MethodDeclaration pNode) {
    if (currentMethod == pNode) {
      currentMethod = null;
    }
  }

  @Override
  public void endVisit(TypeDeclaration pNode) {
    if (currentTypeBinding == pNode.resolveBinding()) {
      // It can happen that we have just left an inner class - to now let's take
      // its declaring class as current type
      currentTypeBinding = pNode.resolveBinding().getDeclaringClass();
    }
  }

  @Override
  public boolean visit(MethodInvocation pNode) {
    String pSourceName = null;

    // First add method call
    if (currentMethod != null) {
      pSourceName = EclipseFactUtils.getMethodFullName(currentMethod);
    } else {
      // It can happen to be that a method is being called outside of any method,
      // e.g. from a variable initializer.
      pSourceName = EclipseFactUtils.getTypeFullName(currentTypeBinding);
    }
    String pTargetName = EclipseFactUtils.getMethodFullName(pNode);
    projectStructureInfo.addRelation(RELATION_NAME_FUNCTIONCALL,
        pSourceName, pTargetName, EclipseFactUtils.getPackageName(pNode));

    // It can happen that a method being called is declared in a super-type that
    // doesn't belong to current project.
    // So we need to add type marker anyway.
    //    projectStructureInfo.addNodeType(pTargetName, NodeType.METHOD);

    // Now add class call
    if (currentMethod != null) {
      pSourceName = EclipseFactUtils.getTypeFullName(currentMethod);
    } else {
      pSourceName = EclipseFactUtils.getTypeFullName(currentTypeBinding);
    }
    pTargetName = EclipseFactUtils.getTypeFullName(pNode);
    projectStructureInfo.addRelation(RELATION_NAME_CLASS_REFERENCE,
        pSourceName, pTargetName, EclipseFactUtils.getPackageName(pNode));

    // Add package call
    if (currentMethod != null) {
      pSourceName = EclipseFactUtils.getPackageName(currentMethod);
    } else {
      pSourceName = EclipseFactUtils.getPackageName(currentTypeBinding);
    }
    pTargetName = EclipseFactUtils.getPackageName(pNode);
    projectStructureInfo.addRelation(RELATION_NAME_PACKAGE_REFERENCE,
        pSourceName, pTargetName, EclipseFactUtils.getPackageName(pNode));

    return false;
  }
}
